﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectStorageDB;
using ProjectStorageLib;

public partial class controls_AddFactor : System.Web.UI.UserControl
{
    protected int NumberOfScales
    {
        get { return (int)ViewState["NumScales"]; }
        set { ViewState["NumScales"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Label newLabel = new Label();
        newLabel.Text = "1.";
        newLabel.Width = 20;
        newLabel.ID = "DynLabel1";

        TextBox newTextBox = new TextBox();
        newTextBox.Width = 180;
        newTextBox.ID = "DynTextBox1";

        RequiredFieldValidator valrFactorScale = new RequiredFieldValidator();
        valrFactorScale.ErrorMessage = "Please, fill scale 1";
        valrFactorScale.ControlToValidate = newTextBox.ID;
        valrFactorScale.Display = ValidatorDisplay.None;

        pScales.Controls.Add(newLabel);
        pScales.Controls.Add(newTextBox);
        pScales.Controls.Add(valrFactorScale);

        if (!Page.IsPostBack)
            this.NumberOfScales = 1;
        else
            CreateControls();
    }

    protected void btnMoreClick(object sender, EventArgs e)
    {
        int scalesCount = 1;
        foreach (Control ctrl in pScales.Controls)
        {
            if (ctrl is Label)
            {
                Label tmpLabel = (Label)ctrl;
                string labelText = tmpLabel.Text;
                int tmpNum = 0;
                if (labelText.Length > 0)
                     tmpNum = Convert.ToInt16(labelText.Substring(0, labelText.Length - 1));
                if (tmpNum > scalesCount)
                    scalesCount = tmpNum;

            }
        }

        Label newLabel = new Label();
        newLabel.Text = (scalesCount + 1).ToString() + ".";
        newLabel.Width = 20;
        newLabel.ID = "DynLabel" + (scalesCount + 1).ToString();

        TextBox newTextBox = new TextBox();
        newTextBox.Width = 180;
        newTextBox.ID = "DynTextBox" + (scalesCount + 1).ToString();

        RequiredFieldValidator valrFactorScale = new RequiredFieldValidator();
        valrFactorScale.ErrorMessage = "Please, fill scale " + (scalesCount + 1).ToString();
        valrFactorScale.ControlToValidate = newTextBox.ID;
        valrFactorScale.Display = ValidatorDisplay.None;

        pScales.Controls.Add(newLabel);
        pScales.Controls.Add(newTextBox);
        pScales.Controls.Add(valrFactorScale);

        this.NumberOfScales ++;
        
    }

    protected void btnSaveClick(object sender, EventArgs e)
    {
        Page.Validate("FactorName");
        if (Page.IsValid)
        {
            Factor newFactor = new ProjectStorageDB.Factor();
            newFactor.Name = tbxFactorName.Text;
            newFactor.CategoryID = Convert.ToInt16(dropdownCategories.SelectedValue);
            newFactor.ScaleType = Convert.ToBoolean(Convert.ToInt32(dropdownScaleType.SelectedValue));

            ProjectStorageLib.Factor.FactorProcessor proc = new ProjectStorageLib.Factor.FactorProcessor();
            proc.AddFactor(newFactor);

            int iter = 0;
            foreach (Control ctrl in pScales.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox tmpTextBox = (TextBox)ctrl;
                    if (tmpTextBox.Text.Length > 0)
                    {
                        FactorScale newFactorScale = new FactorScale();
                        newFactorScale.Value = tmpTextBox.Text;
                        newFactorScale.FactorID = newFactor.FactorID;
                        newFactorScale.Order = iter;                        

                        proc.AddFactorScales(newFactorScale);
                        iter++;
                    }
                }
            }

            pMain.Controls.Clear();
            this.NumberOfScales = 1;
            Label lblSuccess = new Label();
            lblSuccess.Text = "The factor was saved successfully!";
            this.Controls.Add(lblSuccess);
        }
        
    }

    private void CreateControls()
    {
        int count = this.NumberOfScales;
        for (int i = 2; i <= count; i++)
        {
            Label newLabel = new Label();
            newLabel.Text = i.ToString() + ".";
            newLabel.Width = 20;
            newLabel.ID = "DynLabel" + i.ToString();

            TextBox newTextBox = new TextBox();
            newTextBox.Width = 180;
            newTextBox.ID = "DynTextBox" + i.ToString();

            RequiredFieldValidator valrFactorScale = new RequiredFieldValidator();
            valrFactorScale.ErrorMessage = "Please, fill scale " + i.ToString();
            valrFactorScale.ControlToValidate = newTextBox.ID;
            valrFactorScale.Display = ValidatorDisplay.None;

            pScales.Controls.Add(newLabel);
            pScales.Controls.Add(newTextBox);
            pScales.Controls.Add(valrFactorScale);

        }
    }

    protected void CheckFactorName(object source, ServerValidateEventArgs args)
    {
        ProjectStorageLib.Factor.FactorProcessor proc = new ProjectStorageLib.Factor.FactorProcessor();
        args.IsValid = !proc.IsFactorExists(tbxFactorName.Text);
    }

    protected void IsScaleValuesUnique(object source, ServerValidateEventArgs args)
    {
        List<string> scaleValuesList = new List<string>();
        int count = this.NumberOfScales;
        for (int i = 1; i <= count; i++)
        {
            TextBox tbxTmp = (TextBox)FindControl("DynTextBox" + i.ToString());
            scaleValuesList.Add(tbxTmp.Text);
        }

        args.IsValid = (scaleValuesList.Count == scaleValuesList.Distinct().Count());
        
    }

}